﻿using EnumsNET;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
using RearLivestock.Beginning.Api.Application.Command.PigDeath;
using RearLivestock.Beginning.Api.Controllers;
using RearLivestock.Common.Domains.Enum;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.FirstPregnancyReminder
{
    public class GetDeliveryTipsHandler : IRequestHandler<GetDeliveryTipsCommand, Result<PageResultDto<DeliveryTipsDto>>>
    {
        /// <summary>
        /// 栋舍信息仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> bedroomsRepository;
        /// <summary>
        ///  栏位仓储
        /// </summary>
        private readonly IBaseRepository<Fields> fieldsRepository;
        /// <summary>
        /// 配种仓储
        /// </summary>
        private readonly IBaseRepository<PigsBreeding> pigsBreedingRepository;
        /// <summary>
        /// 基本信息耳号仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicInformationRepository;
        /// <summary>
        /// 妊检仓储
        /// </summary>
        private readonly IBaseRepository<PrenatalCheckups> prenatalCheckupsRepository;
        /// <summary>
        /// 分娩仓储
        /// </summary>
        private readonly IBaseRepository<Farrowings> farrowingsRepository;

        public GetDeliveryTipsHandler(IBaseRepository<Bedrooms> bedroomsRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<PigsBreeding> pigsBreedingRepository, IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<PrenatalCheckups> prenatalCheckupsRepository, IBaseRepository<Farrowings> farrowingsRepository)
        {
            this.bedroomsRepository = bedroomsRepository;
            this.fieldsRepository = fieldsRepository;
            this.pigsBreedingRepository = pigsBreedingRepository;
            this.basicInformationRepository = basicInformationRepository;
            this.prenatalCheckupsRepository = prenatalCheckupsRepository;
            this.farrowingsRepository = farrowingsRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<DeliveryTipsDto>>> Handle(GetDeliveryTipsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<DeliveryTipsDto>>() { Code = (int)CommonErrorCode.Search, Data = new PageResultDto<DeliveryTipsDto>() };
            var page = new PageResultDto<DeliveryTipsDto>() { TotalCount = 0, PageCount = 0, List = new List<DeliveryTipsDto>() };
            try
            {
                var list = (from a in (await basicInformationRepository.GetAll())
                            join b in (await bedroomsRepository.GetAll())
                            on a.BedroomId equals b.BedroomId
                            join c in (await fieldsRepository.GetAll())
                            on a.FieldId equals c.FieldId
                            join d in (await pigsBreedingRepository.GetAll())
                            on a.BasicInforId equals d.BasicInforId
                            join e in (await prenatalCheckupsRepository.GetAll())
                            on a.BasicInforId equals e.BasicInforId
                            join f in (await farrowingsRepository.GetAll())
                            on a.BasicInforId equals f.BasicInforId
                            where a.Reproductivestatus == (ReproductivestatusEnums)ReproductivestatusEnums.妊娠 && a.IsPrenatalCheckup == 2
                            select new DeliveryTipsDto()
                            {
                                BasicInforId = a.BasicInforId,
                                EarNumber = a.EarNumber,
                                BedroomId = b.BedroomId,
                                BedroomName = b.BedroomName,
                                FieldId = c.FieldId,
                                FieldName = c.FieldName,
                                PigsBreedingId = d.PigsBreedingId,
                                LastBreedingDate = d.LastBreedingDate,
                                Prompt = "提示",
                                Reproductivestatus = a.Reproductivestatus,
                                IsPrenatalCheckup = a.IsPrenatalCheckup,
                                PrenatalCheckupId = e.PrenatalCheckupId,
                                PregnancyTestDate = e.PregnancyTestDate,
                                FarrowingId = f.FarrowingId,
                                FarrowingDate = f.FarrowingDate
                            }).Distinct().ToList();
                //总条数
                page.TotalCount = list.Count();
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.pageSize);
                page.List = list.OrderBy(d => d.BasicInforId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = page;
                result.Msg = "分娩提示分页列表获取成功";
                result.Code = (int)CommonErrorCode.Success;
            }
            catch (Exception)
            {
                result.Msg = "分娩提示分页列表获取异常";
            }
            return result;
        }
    }
}
